package com.xingfly.model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户角色 一个角色拥有多个权限(角色<-权限 有对多)  一个用户可以拥有多个角色(多对多)
 * Created by SuperS on 2016/11/30.
 */
@Entity
@Table(name = "xf_role")
public class Role extends BaseModel {
    private String roleName;

    @OneToMany(mappedBy = "role", fetch = FetchType.EAGER)
    private List<Permission> permissions;

    @ManyToMany
    @JoinTable(name = "xf_user_role",
            joinColumns = {@JoinColumn(name = "role_id")},
            inverseJoinColumns = {@JoinColumn(name = "user_id")})
    private List<User> users;

    public List<String> getPermissionsName() {
        List<String> list = new ArrayList<>();
        List<Permission> permissions = getPermissions();
        list.addAll(permissions.stream().map(Permission::getPermissionName).collect(Collectors.toList()));
        return list;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public List<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    @Override
    public String toString() {
        return "Role{" +
                "roleName='" + roleName + '\'' +
                ", permissions=" + permissions +
                '}';
    }
}
